package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {
    private final AgencyCertificationAuditMapper agencyCertificationAuditMapper;


    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                                                                         .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                                                                         .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 添加认证信息
     *
     * @param agencyCertificationAuditAddReqDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAgencyCertification(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        // 获取当前用户id
        Long userId = UserContext.currentUserId();
        log.info("当前用户id:{}", userId);
        // // 判断用户是否存在
        // AgencyCertification agencyCertification = this.getById(userId);
        // if (ObjectUtil.isNotNull(agencyCertification)) {
        //     throw new ForbiddenOperationException("当前用户已存在");
        // }
        // 判断当前用户是否已经认证
        AgencyCertification existAgencyCertification = this.lambdaQuery().eq(AgencyCertification::getId, userId)
                                                           .eq(AgencyCertification::getCertificationStatus, 2)
                                                           .one();
        if (ObjectUtil.isNotEmpty(existAgencyCertification)) {
            throw new RuntimeException("当前用户已经认证");
        }
        AgencyCertification agencyCertification = BeanUtil.toBean(agencyCertificationAuditAddReqDTO, AgencyCertification.class);
        agencyCertification.setId(userId);
        agencyCertification.setCertificationStatus(1);
        agencyCertification.setCertificationTime(LocalDateTime.now());
        log.info("添加认证信息:{}", agencyCertification);
        // 添加认证信息
        this.saveOrUpdate(agencyCertification);
        // 添加认证审核信息
        AgencyCertificationAudit agencyCertificationAudit = new AgencyCertificationAudit();
        agencyCertificationAudit.setServeProviderId(userId);
        agencyCertificationAudit.setName(agencyCertificationAuditAddReqDTO.getName());
        agencyCertificationAudit.setIdNumber(agencyCertificationAuditAddReqDTO.getIdNumber());
        agencyCertificationAudit.setLegalPersonName(agencyCertificationAuditAddReqDTO.getLegalPersonName());
        agencyCertificationAudit.setLegalPersonIdCardNo(agencyCertificationAuditAddReqDTO.getLegalPersonIdCardNo());
        agencyCertificationAudit.setBusinessLicense(agencyCertificationAuditAddReqDTO.getBusinessLicense());
        agencyCertificationAudit.setAuditStatus(0);
        agencyCertificationAudit.setCertificationStatus(1);
        log.info("添加认证审核信息:{}", agencyCertificationAudit);

        agencyCertificationAuditMapper.insertAudit(agencyCertificationAudit);

    }

    // @Override
    // @Transactional(rollbackFor = Exception.class)
    // public void rejectReason(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
    //     // 获取当前用户id
    //     Long userId = UserContext.currentUserId();
    //     log.info("当前用户id:{}", userId);
    //     // 重新认证，查找用户修改内容
    //     AgencyCertification agencyCertification = this.getById(userId);
    //     BeanUtil.copyProperties(agencyCertificationAuditAddReqDTO, agencyCertification);
    //     agencyCertification.setCertificationStatus(1);
    //     agencyCertification.setCertificationTime(LocalDateTime.now());
    //     log.info("修改认证信息:{}", agencyCertification);
    //     // 添加认证信息
    //     this.updateById(agencyCertification);
    //
    //     // 添加认证审核信息
    //     AgencyCertificationAudit agencyCertificationAudit = new AgencyCertificationAudit();
    //     agencyCertificationAudit.setServeProviderId(userId);
    //     agencyCertificationAudit.setName(agencyCertificationAuditAddReqDTO.getName());
    //     agencyCertificationAudit.setIdNumber(agencyCertificationAuditAddReqDTO.getIdNumber());
    //     agencyCertificationAudit.setLegalPersonName(agencyCertificationAuditAddReqDTO.getLegalPersonName());
    //     agencyCertificationAudit.setLegalPersonIdCardNo(agencyCertificationAuditAddReqDTO.getLegalPersonIdCardNo());
    //     agencyCertificationAudit.setBusinessLicense(agencyCertificationAuditAddReqDTO.getBusinessLicense());
    //     agencyCertificationAudit.setAuditStatus(0);
    //     agencyCertificationAudit.setCertificationStatus(1);
    //     log.info("添加认证审核信息:{}", agencyCertificationAudit);
    //
    //     agencyCertificationAuditMapper.insertAudit(agencyCertificationAudit);
    // }


}
